package swa.group22.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URISyntaxException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import swa.group22.ProviderApplication;
import swa.group22.ProviderBrokerCallback;
import swa.group22.ProviderService;
import swa2012.telco.broker.beans.BrokerRegistrationStatus;

/**
 * Servlet implementation class ProviderControllServlet
 */
@WebServlet("/ProviderControllServlet")
public class ProviderControllServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;

	private ProviderApplication providerApplication;
	protected JaxWsServerFactoryBean clientFactory;
	protected JaxWsServerFactoryBean callbackFactory;;
	private PrintWriter out; 
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ProviderControllServlet() {
		super();
		// TODO Auto-generated constructor stub
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
			out = response.getWriter();
			
			
				if (request.getParameter("action").equals("startProvider")) {
					
					if(providerApplication == null) {
					
						providerApplication = new ProviderApplication();
						
						/*
						clientFactory = new JaxWsServerFactoryBean();
						callbackFactory = new JaxWsServerFactoryBean();

						
						clientFactory.setServiceClass(ProviderService.class);
						//clientFactory.setAddress("http://student.swa.univie.ac.at:8022/SWA_IMPL_Provider/ProviderService");
						//clientFactory.setAddress("http://192.168.0.10:8080/SWA_IMPL_Provider/ProviderService");
						clientFactory.setServiceBean(providerApplication.getProviderService());
						clientFactory.create();
					
						callbackFactory.setServiceClass(ProviderBrokerCallback.class);
						//callbackFactory.setAddress("http://student.swa.univie.ac.at:8022/SWA_IMPL_Provider/BrokerCallback");
						callbackFactory.setAddress("http://192.168.0.10:8080/SWA_IMPL_Provider/BrokerCallback");
						callbackFactory.setServiceBean(providerApplication.getProviderBrokerCallback());
						callbackFactory.create();
						*/
						
						try {
							out.println("Registering at broker");
							BrokerRegistrationStatus status = providerApplication.registerAtBroker("http://131.130.245.143:8080/SWA_IMPL_Provider/BrokerCallback");
							
							out.println("Server response: "+status.toString());
							
						} catch (URISyntaxException e) {
							// TODO Auto-generated catch block
							out.println("failed at registering"+ e.getMessage());
					}
				} else {
					out.println("already started. ?action=stopProvider first");
				}
					
					
			} else if (request.getParameter("action").equals("stopProvider")) {
				
				if(providerApplication != null) {
					BrokerRegistrationStatus status = providerApplication.deregisterAtBroker();
					out.println("Servlet destroyed. Broker response: "+status.toString());
					//providerApplication.destroy();
					providerApplication = null;
					//clientFactory.destroy();
					//callbackFactory.destroy();	
				} else {
					out.println("already destroyed");
				}
			}
		
	}
	
	
}

